<template>
  <div class="goods-item" @click="itemClick">
    <!-- @load监听图片加载 -->
    <!-- <img :src="showImage" alt="" @load="imageLoad" /> -->
    <!-- 3.图片懒加载修改img :src -> v-lazy -->
    <img v-lazy="showImage" alt="" @load="imageLoad" />
    <div class="goods-info">
      <p>{{ goodsItem.title }}</p>
      <span class="price">￥{{ goodsItem.price }}</span>
      <span class="collect">{{ goodsItem.cfav }}</span>
    </div>
  </div>
</template>

<script>
export default {
  name: "GoodsListItem",
  props: {
    goodsItem: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  computed: {
    showImage() {
      return (
        this.goodsItem.img || this.goodsItem.image || this.goodsItem.show.img
      );
    },
  },
  methods: {
    // 图片加载完就执行这个函数
    imageLoad() {
      // 事件总线 发射:this.$bus.$emit('aaaa')  接收:this.$bus.$on('aaaa',function)
      this.$bus.$emit("itemImageLoad");
    },
    itemClick() {
      // 跳转到详情页
      this.$router.push("/detail/" + this.goodsItem.iid);
      /* this.$router.push({
        path: "/detail",
        query: {
          iid: this.goodsItem.iid,
        },
      }); */
    },
  },
};
</script>

<style>
.goods-item {
  width: 46%;
  margin-bottom: 5px;
}

.goods-item img {
  width: 100%;
  border-radius: 10px;
}

.goods-item p {
  /* 文字只显示两行，多余部分省略号显示 */
  text-overflow: -o-ellipsis-lastline;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;

  font-size: 14px;
}

.price {
  font-size: 12px;
  color: #ff699c;
  margin-left: 10%;
}

.collect {
  position: relative;
  margin-left: 20%;
}

.goods-info .collect::before {
  content: "";
  position: absolute;
  left: -15px;
  top: -1px;
  width: 14px;
  height: 14px;
  background: url("~assets/img/common/collect.svg") 0 0/14px 16px;
}
</style>